package com.example.lt.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.lt.authority.Authority;
import com.example.lt.constant.AuditStatus;
import com.example.lt.constant.MessageConstants;
import com.example.lt.entity.queryPage.BasePage;
import com.example.lt.entity.user.User;
import com.example.lt.entity.video.Type;
import com.example.lt.entity.video.Video;
import com.example.lt.entity.vo.VideoStatistics;
import com.example.lt.service.user.UserService;
import com.example.lt.service.video.TypeService;
import com.example.lt.service.video.VideoService;
import com.example.lt.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 小涛Tao
 * @Date: 2024/08/17/19:53
 * version 1.0 注释：管理端视频控制器
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/video")
public class AdminVideoController {

    private final VideoService videoService;

    private final UserService userService;

    private final TypeService typeService;

    /**
     * 管理端根据id获取视频
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        log.info("管理端根据id获取视频：{}", id);
        return Result.ok().data(videoService.getVideoById(id, null));
    }

    /**
     * 管理端删除视频
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        log.info("管理端删除视频：{}", id);
        videoService.deleteVideo(id);
        return Result.ok().message(MessageConstants.DELETE_SUCCESS);
    }

    /**
     * 管理端放行视频
     * @param video
     * @return
     */
    @PostMapping("/audit")
    public Result audit(@RequestBody Video video) {
        log.info("管理端放行视频：{}", video.getId());
        videoService.auditProcess(video);
        return Result.ok().message("审核放行");
    }

    /**
     * 分页获取视频
     * @param basePage 分页信息
     * @param YV 视频yv
     * @param title 视频标题
     * @return
     */
    @GetMapping("/page")
    public Result page(BasePage basePage, @RequestParam(required = false) String YV, @RequestParam(required = false) String title) {
        log.info("分页获取视频");
        final LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(!ObjectUtils.isEmpty(YV), Video::getYv, YV).like(!ObjectUtils.isEmpty(title), Video::getTitle, title);

        final IPage<Video> page = videoService.page(basePage.page(), wrapper);

        final List<Video> records = page.getRecords();
        if (ObjectUtils.isEmpty(records)) return Result.ok();

        final List<Long> userIds = records.stream().map(Video::getUserId).collect(Collectors.toList()); // 抽取所有视频的用户id
        final List<Long> typeIds = records.stream().map(Video::getTypeId).collect(Collectors.toList()); // 抽取所有视频的分类id

        // 查询用户名称，并将【用户id和名称】转换成Map集合，以便后续【视频设置用户名称】
        final Map<Long, String> userMap = userService.list(new LambdaQueryWrapper<User>().select(User::getId, User::getNickName).in(User::getId, userIds))
                .stream().collect(Collectors.toMap(User::getId, User::getNickName));
        // 查询分类名称，并将【分类id和名称】转换成Map集合，以便后续【视频设置分类名称】
        final Map<Long, String> typeMap = typeService.list(new LambdaQueryWrapper<Type>().select(Type::getId, Type::getName).in(Type::getId, typeIds))
                .stream().collect(Collectors.toMap(Type::getId, Type::getName));

        for (Video video : records) {
            video.setUserName(userMap.get(video.getUserId())); // 设置用户名字
            video.setTypeName(typeMap.get(video.getTypeId())); // 设置分类名字
            video.setAuditStateName(AuditStatus.getName(video.getAuditStatus())); // 设置审核状态名称
            video.setOpenName(video.getOpen() ? "私密" : "公开"); // 设置视频的 私密/公开 为文字
        }

        return Result.ok().data(records).count(page.getTotal());
    }

    /**
     * 视频数据统计
     * @return
     */
    @GetMapping("/statistics")
    @Authority("admin:video:statistics")
    public Result show() {
        log.info("视频数据统计");
        final VideoStatistics videoStatistics = new VideoStatistics();
        final int allCount = videoService.count(new LambdaQueryWrapper<Video>());
        final int processCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.PROCESS));
        final int successCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.SUCCESS));
        final int passCount = videoService.count(new LambdaQueryWrapper<Video>().eq(Video::getAuditStatus, AuditStatus.PASS));

        videoStatistics.setAllCount(allCount);
        videoStatistics.setProcessCount(processCount);
        videoStatistics.setSuccessCount(successCount);
        videoStatistics.setPassCount(passCount);

        return Result.ok().data(videoStatistics);
    }


}
